import 'package:dog_read/models/book/book_model.dart';
import 'package:flutter/material.dart';
import '../sites/biquge.dart';
import 'package:dog_read/components/dotted_separator.dart';

class BookHome extends StatefulWidget {
  @override
  _BookHomeState createState() => _BookHomeState();
}

class _BookHomeState extends State<BookHome>
    with AutomaticKeepAliveClientMixin<BookHome> {
  Future _recommendBuilder;

  @override
  void initState() {
    _recommendBuilder = BiqugeSpider.getAllRecommend();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return FutureBuilder(
        future: _recommendBuilder,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState != ConnectionState.done) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
          List hotContentList = snapshot.data["hotRecommend"];
          List hotRecommendBooks = snapshot.data["strongRecommend"];
          return Container(
              margin: EdgeInsets.all(8.0),
              child: ListView(
                // shrinkWrap: true,
                padding: EdgeInsets.all(0.0),
                children: [
                  HotBooks(
                    hotBooks: hotContentList,
                    hotRecommendBooks: hotRecommendBooks,
                  )
                ],
              ));
        });
  }

  @override
  bool get wantKeepAlive => true;
}

class HotBooks extends StatelessWidget {
  final List<BookModel> hotBooks;
  final List<BookModel> hotRecommendBooks;

  HotBooks({this.hotBooks, this.hotRecommendBooks});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        RecommendBookList(hotBooks),
        StrongRecommendBookList(hotRecommendBooks)
      ],
    );
  }
}

class StrongRecommendBookList extends StatelessWidget {
  final List<BookModel> hotRecommendBooks;
  StrongRecommendBookList(this.hotRecommendBooks);

  Widget _getRecommendbooks(BuildContext context, int index) {
    return Column(children: <Widget>[
      ListTile(
        onTap: () {
          Navigator.pushNamed(context, "/book/detail", arguments: {
            "url": this.hotRecommendBooks[index].bookUrl,
            "title": this.hotRecommendBooks[index].title
          });
        },
        leading: Text(this.hotRecommendBooks[index].bookType),
        title: Text(
          this.hotRecommendBooks[index].title,
          style: TextStyle(
              fontSize: 18.0,
              fontWeight: FontWeight.w300,
              color: Colors.lightBlue),
        ),
        trailing: Text(
          this.hotRecommendBooks[index].author,
          style: TextStyle(color: Colors.grey),
        ),
      ),
      MySeparator(
        color: index != this.hotRecommendBooks.length - 1
            ? Colors.grey
            : Colors.white,
      )
    ]);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        SizedBox(
          height: 20,
        ),
        Container(
          margin: EdgeInsets.only(left: 8.0),
          child: Text(
            "强烈推荐",
            style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w300),
          ),
        ),
        Card(
          child: ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: this.hotRecommendBooks.length,
            itemBuilder: _getRecommendbooks,
          ),
        )
      ],
    );
  }
}

class RecommendBookList extends StatelessWidget {
  final List<BookModel> hotBooks;
  RecommendBookList(this.hotBooks);

  Widget _getHotbooks(BuildContext context, int index) {
    return GestureDetector(
        onTap: () {
          Navigator.pushNamed(context, "/book/detail", arguments: {
            "url": this.hotBooks[index].bookUrl,
            "title": this.hotBooks[index].title
          });
        },
        child: Card(
            child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Expanded(
                flex: 1,
                child: AspectRatio(
                  aspectRatio: 4 / 5,
                  child: Image.network(
                    this.hotBooks[index].imageUrl,
                    fit: BoxFit.cover,
                  ),
                )),
            SizedBox(
              width: 8,
            ),
            Expanded(
              flex: 3,
              child: Container(
                  child: Column(children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text(
                      this.hotBooks[index].title,
                      style: TextStyle(
                          color: Colors.lightBlue,
                          fontWeight: FontWeight.w300,
                          fontSize: 16),
                    ),
                    Text(this.hotBooks[index].author,
                        style: TextStyle(color: Colors.grey))
                  ],
                ),
                MySeparator(
                  color: Colors.grey[300],
                ),
                Text(
                  this.hotBooks[index].description,
                  overflow: TextOverflow.ellipsis,
                  maxLines: 4,
                )
              ])),
            ),
            SizedBox(
              width: 8,
            ),
          ],
        )));
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(left: 8.0),
          child: Text(
            "热门推荐",
            style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w300),
          ),
        ),
        MediaQuery.removePadding(
            removeTop: true,
            context: context,
            child: ListView.builder(
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              itemCount: this.hotBooks.length,
              itemBuilder: _getHotbooks,
            )),
      ],
    );
  }
}
